/**
 * created by dt at 2025/5/7
 */

import { Entity } from "../../../core/Entity";

// Buff类型枚举
export enum BuffType {
    Attribute = 0,    // 属性修改
    State = 1,        // 状态效果
    Periodic = 2,     // 周期性效果
    Trigger = 3,      // 条件触发效果
    Composite = 4,    // 复合效果
}

// Buff堆叠类型
export enum StackType {
    None = 0,         // 不可堆叠
    Stack = 1,        // 可堆叠
    Refresh = 2,      // 刷新持续时间
    Independent = 3,  // 独立堆叠
}

// Buff标签枚举 - 使用位掩码 最大支持31个标签
export enum BuffTag {
    // 功能分类 (0-7)
    Defense = 1 << 0,     // 防御类
    Attack = 1 << 1,      // 攻击类
    Control = 1 << 2,     // 控制类
    Movement = 1 << 3,    // 移动类
    Recovery = 1 << 4,    // 恢复类
    
    // 效果分类 (16-31)
    Buff = 1 << 16,        // 增益效果
    Debuff = 1 << 17,      // 减益效果
    Special = 1 << 18,    // 特殊效果

    // 免疫标签 (8-15)
    ImmuneControl = 1 << 8,    // 免疫控制
    ImmuneDebuff = 1 << 9,     // 免疫减益
    ImmuneMovement = 1 << 10,  // 免疫移动限制
    ImmuneAll = 1 << 11,       // 免疫所有效果
}

export class Buff {
    private readonly _id: number;
    private readonly _type: BuffType;
    private readonly _stackType: StackType;
    private readonly _maxStacks: number;
    private readonly _priority: number;
    private _duration: number;  // -1表示永久
    private _tags: number;
    
    public stacks: number = 1;
    public remainingTime: number = 0;
    public isActive: boolean = true;
    public targetEntity?: Entity;  // 目标实体

    constructor(
        id: number,
        type: BuffType,
        stackType: StackType,
        maxStacks: number,
        priority: number,
        duration: number,
        tags: number = 0
    ) {
        this._id = id;
        this._type = type;
        this._stackType = stackType;
        this._maxStacks = maxStacks;
        this._priority = priority;
        this._duration = duration;
        this._tags = tags;
        this.remainingTime = duration;
    }

    public update(deltaTime: number): void {
        if (this._duration > 0) {
            this.remainingTime -= deltaTime;
            if (this.remainingTime <= 0) {
                this.isActive = false;
            }
        }   
    }

    public refresh(): void {
        if (this._duration > 0) {
            this.remainingTime = this._duration;
        }
    }

    public addStack(): boolean {
        if (this._stackType === StackType.None) {
            return false;
        }
        if (this._stackType === StackType.Refresh) {
            this.refresh();
            return true;
        }
        if (this.stacks < this._maxStacks) {
            this.stacks++;
            return true;
        }
        return false;
    }

    // 检查是否包含指定标签
    public hasTag(tag: BuffTag): boolean {
        return (this._tags & tag) !== 0;
    }

    // 检查是否包含任意一个指定标签
    public hasAnyTag(tags: number): boolean {
        return (this._tags & tags) !== 0;
    }

    // 检查是否包含所有指定标签
    public hasAllTags(tags: number): boolean {
        return (this._tags & tags) === tags;
    }

    // 添加标签
    public addTag(tag: BuffTag): void {
        this._tags |= tag;
    }

    // 移除标签
    public removeTag(tag: BuffTag): void {
        this._tags &= ~tag;
    }
}